package com.example.superoa.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.model.pojos.KqAttendance;
import com.example.superoa.model.pojos.KqBusiness;
import com.example.superoa.model.pojos.Staff;
import com.example.superoa.model.service.BusinessService;
import com.example.superoa.model.service.DemoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class BusinessController {
	@Autowired
	private BusinessService ser;
	
	@Autowired
	private DemoService d_ser;
	
	
	//kq-business-assessor 页面table初始化值
	@RequestMapping("business_selectAll")
	@ResponseBody
	public Map<String,Object> LeaveSelectAll(int page,int rows) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
		JSONArray json = ser.selectAll();
		int i=1;
		for (Object object : json) {
			JSONObject obj=(JSONObject) object;
			int shen=obj.getInteger("bus_shen");
			String str="";
			if(shen==0) {
				str="未审核";
			}else if(shen==1) {
				str="通过";
			}else {
				str="未通过";
			}
			obj.put("b_sta", str);
			obj.put("sortable", i);
			i++;
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", json);
		return map;
	}
	@RequestMapping("business-shen")
	public void selectShen() {
		
	}
	@RequestMapping("select-business-pan")
	@ResponseBody
	public Integer selectPan(Date kai,Date jie,HttpSession session) {
		return ser.selectPan(kai,jie,Integer.parseInt(session.getAttribute("sid").toString()));
	}
	
	@RequestMapping("addbusiness-shen")
	public void addchu(KqBusiness buiness) {
		ser.addChu(buiness,1);
	}
	//kq-business-statistical 页面table初始化值
		@RequestMapping("bstatis_selectAll")
		@ResponseBody
		public Map<String,Object> statisSelectAll(int page,int rows) {
			Map<String,Object> map = new HashMap<>();
			//先分页
			Page<Object> pg = PageHelper.startPage(page, rows);
			JSONArray json =new JSONArray();
			JSONArray list=d_ser.selectKuang();
			int i=1;
			for (Object o : list) {
				JSONObject obj=new JSONObject();
				JSONObject ob=(JSONObject)o;
				Integer one=0;
				if(ser.selectCount(ob.getInteger("staff_id"))!=null) {
					one=ser.selectCount(ob.getInteger("staff_id"));
				}
				obj.put("sortable", i);
				obj.put("staff_id", ob.getInteger("staff_id"));
				obj.put("Staff_name", ob.getString("Staff_name"));
				obj.put("depart_name", ob.getString("depart_name"));
				obj.put("one",one);
				i++;
				json.add(obj);
			}
			//总记录
			map.put("total", pg.getTotal());
			//页内容
			map.put("rows", json);
			return map;
		}
		//kq-business-shen 出差申请提交
		@RequestMapping("add_business")
		@ResponseBody
		public String add_business(KqBusiness pojo,HttpSession session) {
			ser.addChu(pojo,Integer.parseInt(session.getAttribute("sid").toString()));
			return "";
		}
		//kq-business-assessor  查看
		@RequestMapping("kq-business-cha")
		public void cha(Integer id,Model model) {
			JSONObject bus=ser.selectId(id);
			Staff staff=d_ser.selectStaffid(bus.getInteger("staff_id"));
			model.addAttribute("bus", bus);
			model.addAttribute("staff", staff);
		}
}
